<?php
/**
 * Description of apiAction
 *
 * @author Administrator
 */
class apiAction extends BaseAction{

    //检查权限
    private function is_public($uhash) {
        
        if(empty($uhash)) {
            echo '暂时没有统计数据';
            exit;
        }

       // $key    = 'url_public_'.$uhash;
//        $cache  = MemcacheAction::getCache();
//        $public = $cache->get($key);
        $public = null;
        if(!$public) {
            $rs = DAO_Finder::findOne(DB_URL, array('uhash' => $uhash), 'status,uid');
            if(!$rs) {
                //$cache->set($key, 'error');
                echo '该短网址不存在'.$uhash;
                exit;
            }
            $uid    = $rs->getUid();
            //$cache->set($key, $public, 3600);
            $public = trim($rs->getStatus());
            if($public !='public' && $uid != $_SESSION['user']['id']) {
                echo '该网址设置保密';
                exit;
            }
        }
        if($public =='error') {
            echo '该短网址不存在';
            exit;
        }

    }
    
    function actionHour() {
        $uhash      = trim($this->params['uhash']);
        $key_ip    = 'count_hour_'.$uhash.'_ip';
        $key_pv    = 'count_hour_'.$uhash.'_pv';
        $key_y1    = 'count_hour_'.$uhash.'_y1';
        $max_y_key  = 'count_hour_'.$uhash.'_max_y';

//        $cache  = MemcacheAction::getCache();
//        $ip    =    $cache->get($key_ip);
//        $pv    =    $cache->get($key_pv);
//        $y1    =    $cache->get($key_y1);
//        $max_y =    $cache->get($max_y_key);
        $ip = null;
        if(!$ip) {
            $ip = array();
            $pv = array();
            $y1 = array();
            load_help('DateTime');
            $hour   = getHourCount(3600*11,'-',12);
            asort($hour);
            $max_y    = 10;

            //获取pv
            foreach ($hour as $k=>$v):
                $date   = date('G',$v['start']);
                if ($date==0) {
                    $date   = 24;
                }
                $y1[]   = $date.'时';
                $tmp   = (int) DB_ACCESS::getPv($uhash, $v['start'], $v['end']);
                $pv[]     = $tmp;
                $ip[]   = (int) DB_IP::getIp($uhash, $v['start'], $v['end']);
                if($tmp > $max_y){
                    $max_y  = $tmp;
                }
                //$dataset[]    = array('pv'=>"$date",'ip'=> "$ip",'pv'=>"$pv");
            endforeach;
//            $life   = 60 * 30;
//            $cache->set($key_ip, $ip, $life);
//            $cache->set($key_pv, $pv, $life);
//            $cache->set($key_y1, $y1, $life);
//            $cache->set($max_y_key, $max_y, $life);
        }
        //print_r($pv);
        //开始绘图
        $step   = round($max_y / 10,0);
        if($max_y > 10) {
            $max_y  = $max_y + $step;
        }
        //$max_y  = $max_y;
        require_once('OFC/OFC_Chart.php');
        $title = new OFC_Elements_Title('本月PV、IP统计' );

        $bar = new OFC_Charts_Bar();
        $bar->set_values($pv);
        $bar->set_tooltip('该时段有PV: #val#');

        $bar2   = new OFC_Charts_Bar();
        $bar2->set_values($ip);
        $bar2->set_colour('ff0000');
        $bar2->set_tooltip('该时段有IP: #val#');

        $x_x    = new OFC_Elements_Axis_X();
        $x_x->set_labels_from_array($y1);

        $y      = new OFC_Elements_Axis_Y();
        $y->set_offset(true);
        $y->set_range(0, $max_y, $step);


        $y_l    = new OFC_Elements_Legend_Y('test');

        $chart = new OFC_Chart();
        $chart->set_title( $title );
        $chart->add_element($bar);
        $chart->add_element($bar2);
        $chart->set_x_axis($x_x);
        $chart->set_y_axis($y);
        $chart->set_bg_colour('ffffff');
        echo $chart->toPrettyString();

    }
    
    function actionHourNew() {
        $uhash      = trim($this->params['uhash']);
        $key_ip    = 'count_hour_'.$uhash.'_ip';
        $key_pv    = 'count_hour_'.$uhash.'_pv';
        $key_y1    = 'count_hour_'.$uhash.'_y1';
        $max_y_key  = 'count_hour_'.$uhash.'_max_y';

//        $cache  = MemcacheAction::getCache();
//        $ip    =    $cache->get($key_ip);
//        $pv    =    $cache->get($key_pv);
//        $y1    =    $cache->get($key_y1);
//        $max_y =    $cache->get($max_y_key);
        $ip = null;
        if(!$ip) {
            $ip = array();
            $pv = array();
            $y1 = array();
            load_help('DateTime');
            $hour   = getHourCount(3600*11,'-',12);
            asort($hour);
            $max_y    = 10;
            
            //获取pv
            foreach ($hour as $k=>$v):
                $date       = date('G',$v['start']);
                $log_time   = date('YmdH',$v['start']);

                if ($date==0) {
                    $date   = 24;
                }
                $y1[]       = $date.'时';
                $tmp        = (int)DB_COUNT_PV_HOUR::getCount($uhash, $log_time);
                $pv[]       = $tmp;
                $ip[]       = (int)DB_COUNT_IP_HOUR::getCount($uhash, $log_time);
                if($tmp > $max_y){
                    $max_y  = $tmp;
                }
                //$dataset[]    = array('pv'=>"$date",'ip'=> "$ip",'pv'=>"$pv");
            endforeach;
//            $life   = 60 * 30;
//            $cache->set($key_ip, $ip, $life);
//            $cache->set($key_pv, $pv, $life);
//            $cache->set($key_y1, $y1, $life);
//            $cache->set($max_y_key, $max_y, $life);
        }
        //print_r($pv);
        //开始绘图
        $step   = round($max_y / 10,0);
        if($max_y > 10) {
            $max_y  = $max_y + $step;
        }
        //$max_y  = $max_y;
        require_once('OFC/OFC_Chart.php');
        $title = new OFC_Elements_Title('本月PV、IP统计' );

        $bar = new OFC_Charts_Bar();
        $bar->set_values($pv);
        $bar->set_tooltip('该时段有PV: #val#');

        $bar2   = new OFC_Charts_Bar();
        $bar2->set_values($ip);
        $bar2->set_colour('ff0000');
        $bar2->set_tooltip('该时段有IP: #val#');

        $x_x    = new OFC_Elements_Axis_X();
        $x_x->set_labels_from_array($y1);

        $y      = new OFC_Elements_Axis_Y();
        $y->set_offset(true);
        $y->set_range(0, $max_y, $step);


        $y_l    = new OFC_Elements_Legend_Y('test');

        $chart = new OFC_Chart();
        $chart->set_title( $title );
        $chart->add_element($bar);
        $chart->add_element($bar2);
        $chart->set_x_axis($x_x);
        $chart->set_y_axis($y);
        $chart->set_bg_colour('ffffff');
        echo $chart->toPrettyString();

    }
    
    function actionDay() {
        $uhash  = trim($this->params['uhash']);
        $ip_key = 'month_count_ip_'.$uhash;
        $pv_key = 'month_count_pv_'.$uhash;
        $y1_key = 'month_count_day_'.$uhash;
        $max_y_key = 'month_count_max_y_'.$uhash;
//        $mem    = MemcacheAction::getCache();
//
//        $ip     = $mem->get($ip_key);
//        $pv     = $mem->get($pv_key);
//        $y1     = $mem->get($y1_key);
//        $max_y  = $mem->get($max_y_key);
        $ip = null;
        if(!$ip){
            $ip = array();
            $pv = array();
            $y1 = array();
            load_help('DateTime');
            $id = $this->params['id'];
            $time_stamp = 3600*24 * 13;
            $hour   = getMonthCount($time_stamp, '-',14);
            asort($hour);
            //print_r($hour);
            $max_y  = 10;
            //获取pv
            foreach ($hour as $k=>$v):
                $y1[]   =  substr($v['start'],6,2).'日';
                //$pv[]   = DB_ACCESS::getPv($uhash, $v['start'], $v['end']);
                $tmp_pv = (int)  DB_COUNT_PV::getCount($uhash, $v['start']);
                if($tmp_pv > $max_y) {
                    $max_y  = $tmp_pv;
                }
                $pv[]   = $tmp_pv;
                $ip[]   = (int)  DB_COUNT_IP::getCount($uhash, $v['start']);
                //echo date('Y-m-d H:i:s',$v['start']).'--'.date('Y-m-d H:i:s',$v['end']).'<br/>';
            endforeach;
//            $life    = 3600*12;
//            $mem->set($ip_key, $ip, $life);
//            $mem->set($pv_key, $pv, $life);
//            $mem->set($y1_key, $y1, $life);
//            $mem->set($max_y_key, $max_y, $life);
        }

        //开始绘图
        $step   = round($max_y / 10,0);
        if($max_y > 10) {
            $max_y  = $max_y + $step;
        }
        require_once('OFC/OFC_Chart.php');
        $title = new OFC_Elements_Title('本月PV、IP统计' );

        $bar = new OFC_Charts_Bar();
        $bar->set_values( $pv);
        $bar->set_tooltip('该日有PV: #val#');

        $bar2   = new OFC_Charts_Bar();
        $bar2->set_values($ip);
        $bar2->set_colour('ff0000');
        $bar2->set_tooltip('该日有IP: #val#');

        $x_x    = new OFC_Elements_Axis_X();
        $x_x->set_labels_from_array($y1);
        
        $y      = new OFC_Elements_Axis_Y();
        $y->set_offset(true);
        $y->set_range(0, $max_y, $step);


        $y_l    = new OFC_Elements_Legend_Y('test');

        $chart = new OFC_Chart();
        $chart->set_title( $title );
        $chart->add_element($bar2);
        $chart->add_element($bar);
        $chart->set_x_axis($x_x);
        $chart->set_y_axis($y);
        $chart->set_bg_colour('ffffff');
        echo $chart->toPrettyString();
        
    }


    function actionIe() {

        $uhash  = trim($this->params['uhash']);
        $key    = 'ie_'.$uhash;
//        $mem  = MemcacheAction::getCache();
//        $arr    = $mem->get($key);
        $arr    = null;
        if(!$arr) {
            $rs     = DAO_Finder::find(DB_URL_IE, array('uhash'=>$uhash), 'name,click');

            $arr    = array();
            foreach ($rs as $k => $v):
                $v  = $v->getArray();
                if(empty($v['name'])) {
                    $v['name']   =0;
                }
                if(empty($v['click'])) {
                    $v['click']   =0;
                }
                //$arr[]    = "['".$v['name']."',".$v['click']."]";
                $arr[]    = new OFC_Charts_Pie_Value((int)$v['click'],$v['name']);
            endforeach;
            //$mem->set($key, $arr,3600);
        }

        //绘图开始
        require_once('OFC/OFC_Chart.php');
        $title = new OFC_Elements_Title( '浏览器比例统计' );

        $pie = new OFC_Charts_Pie();
        $pie->set_start_angle($angle);
        $pie->set_animate( true );
        $pie->values = $arr;

        $pie->tip   = '浏览器:#label#占有#val# 总数#total# 占总数比例:#percent#';
        //$pie->set_label('BIG', '#FF653F', 24 );

        $chart = new OFC_Chart();
        $chart->set_title( $title );
        $chart->add_element( $pie );
        $chart->x_axis = null;
        $chart->set_bg_colour('ffffff');
        echo $chart->toPrettyString();
    }

    function actionOs() {

        $uhash  = trim($this->params['uhash']);
        $key    = 'os_'.$uhash;
//        $mem  = MemcacheAction::getCache();
//        $arr    = $mem->get($key);
        $arr    = null;
        if(!$arr) {
            $rs     = DAO_Finder::find(DB_URL_OS, array('uhash'=>$uhash), 'name,click');

            $arr    = array();
            foreach ($rs as $k => $v):
                $v  = $v->getArray();
                if(empty($v['name'])) {
                    $v['name']   =0;
                }
                if(empty($v['click'])) {
                    $v['click']   =0;
                }
                //$arr[]    = "['".$v['name']."',".$v['click']."]";
                $arr[]    = new OFC_Charts_Pie_Value((int)$v['click'],$v['name']);
            endforeach;
            //$mem->set($key, $arr,3600);
        }

        //绘图开始
        require_once('OFC/OFC_Chart.php');
        $title = new OFC_Elements_Title( '操作系统比例统计' );
        $pie = new OFC_Charts_Pie();
        $pie->set_start_angle($angle);
        $pie->set_animate( true );
        $pie->values = $arr;
        $pie->tip   = '操作系统:#label#占有#val# 总数#total# 占总数比例:#percent#';
        $chart = new OFC_Chart();
        $chart->set_title( $title );
        $chart->add_element( $pie );
        $chart->x_axis = null;
        $chart->set_bg_colour('ffffff');
        echo $chart->toPrettyString();
    }

    function actionFlashHour(){
        
        $uhash  = trim($this->params['uhash']);
        $width = $this->params['width'];
        $this->is_public($uhash);
        include_once 'ofc-library/open-flash-chart-object.php';
        if(empty($width)) {
            $width  = 958;
        }
        $url    = 'http://'. $_SERVER['HTTP_HOST'] .'/api/hour/uhash/'.$uhash;
        open_flash_chart_object( $width, 200, $url,true, url_for('misc/flash/'));
    }

    function actionFlashHourNew(){
        
        $uhash  = trim($this->params['uhash']);
        $width = $this->params['width'];
        $this->is_public($uhash);
        include_once 'ofc-library/open-flash-chart-object.php';
        if(empty($width)) {
            $width  = 958;
        }
        $url    = 'http://'. $_SERVER['HTTP_HOST'] .'/api/hour_new/uhash/'.$uhash;
        open_flash_chart_object( $width, 200, $url,true, url_for('misc/flash/'));
    }
    
    function actionFlashDay(){
        $uhash  = trim($this->params['uhash']);
        $width = $this->params['width'];
        $this->is_public($uhash);
        include_once 'ofc-library/open-flash-chart-object.php';
        
        if(empty($width)) {
            $width  = 958;
        }
        $url    = 'http://'. $_SERVER['HTTP_HOST'] .'/api/day/uhash/'.$uhash;
        open_flash_chart_object( $width, 200, $url,true, url_for('misc/flash/'));
    }
    
    function actionFlashIe(){
        $uhash  = trim($this->params['uhash']);
        $width = $this->params['width'];
        $this->is_public($uhash);
        include_once 'ofc-library/open-flash-chart-object.php';
        
        if(empty($width)) {
            $width  = 958;
        }
        $url    = 'http://'. $_SERVER['HTTP_HOST'] .'/api/ie/uhash/'.$uhash;
        open_flash_chart_object( $width, 200, $url,true, url_for('misc/flash/'));
    }

    function actionFlashOs(){
        $uhash  = trim($this->params['uhash']);
        $width = $this->params['width'];
        $this->is_public($uhash);
        include_once 'ofc-library/open-flash-chart-object.php';
        
        if(empty($width)) {
            $width  = 958;
        }
        $url    = 'http://'. $_SERVER['HTTP_HOST'] .'/api/os/uhash/'.$uhash;
        open_flash_chart_object( $width, 200, $url,true, url_for('misc/flash/'));
    }

    /**
     * 生成短连接
     */
    function actionPost(){
        
        $sina_url   = null;
        
        $uid    = !array_key_exists('user', $_SESSION)?0:$_SESSION['user']['id'];

        $url    = trim($this->postVar('url'));
        
        if(empty($url) || !preg_match('~http://~', $url)) {
            setFlashMsg('错误的网址');
            $this->redirect($_SERVER['HTTP_REFERER']);
        }else{
            $back_url    = $this->postVar('back_url');
            
//            $uhash  = DB_URL::getHash($url);
//            if(null==$uhash){
//                setFlashMsg('错误的网址');
//                $this->redirect($_SERVER['HTTP_REFERER']);                
//            }
            if($url=='http://'){
                $this->redirect(url_for($back_url));
            }
            //添加操作
            $save   = DB_URL::add($url,-999, $uid, $sina_url);
            $uhash  = $save->getUhash();

            if($uid ==0) {
                Cookies::setMine($uhash);
            }
        }
        
        //新浪短连接
        if(array_key_exists('sina', $_POST)) {
            $re = trim(get_action('front', 'sina',array('url' =>'http://'.$_SERVER['HTTP_HOST'].'/'.$uhash)));
            $re = json_decode($re);
            $sina_url   = trim($re->url);
            
            //短链接在微博上的微博评论数
            $comment    = DB_SHORT_URL_COMMENT_COUNTS::created_record($sina_url);
            
            //短链接在微博上的微博分享数
            $share      = DB_SHORT_URL_SHARE_COUNTS::created_record($sina_url);
            
            //更新添加记录
            $has    = DAO_Finder::findOne(DB_URL, array('uhash' =>$uhash));
            if($has) {
                $has->setSinaUrl($sina_url);
                $has->save();
            }
        }
        

        
        $this->redirect(url_for($back_url));
    }


    
}
?>
